import sqlite3 
import sys
import json
from funcs import *
from PyQt5.QtWidgets import QWidget,QVBoxLayout,QHBoxLayout,QPushButton,QMainWindow,QApplication,QStyleFactory,QLabel,QTextEdit,QTreeWidget,QTreeWidgetItem,QLineEdit,QAction,qApp,QTabWidget
from PyQt5.Qt import QFont,QIcon
from widgets import *

class MainWindow(QMainWindow):
	def __init__(self):
		super().__init__()
		self.initUI()
		self.addSourceActions()
	def initUI(self):	
		self.sourceCreateWidget = sourceCreateWidget()
		self.taskAdder = taskAdder()
		self.accomplishTaskWidget = accomplishTaskWidget()
		self.settingsWidget = settingsWidget()

		self.tabWidget = QTabWidget()
		self.tabWidget.addTab(self.sourceCreateWidget,"添加字音")
		self.tabWidget.addTab(self.taskAdder,"添加任务")
		self.tabWidget.addTab(self.accomplishTaskWidget,"完成任务")
		self.tabWidget.addTab(self.settingsWidget,"设置与工具")
		self.tabWidget.currentChanged.connect(self.switchWidget)

		#self.switchWidget(2)
		#self.tabWidget.setCurrentIndex(2)

		self.setCentralWidget(self.tabWidget)
		self.setGeometry( 20, 30,1200, 670)
		self.setWindowTitle('语文第一题训练工具')
		self.setWindowIcon(QIcon('iconbmp/Edit page.bmp'))
		self.show()
	def switchWidget(self,index):
		if index == 0:
			self.addSourceActions()
		if index == 1:
			self.menuBar().clear()
			self.taskAdder.getSourceTables()
			self.taskAdder.updateSourceTables(True)
		if index == 2:
			self.menuBar().clear()
			self.accomplishTaskWidget.getTasks()
			self.accomplishTaskWidget.updateTasks()
	def addSourceActions(self):
		exitAct = QAction('&Exit', self)
		tableAct = QAction('&Goto table', self)
		hanAct = QAction('&Goto han', self)
		py1Act = QAction('&Goto pinyin1', self)
		pycAct = QAction('&Goto pinyincommon', self)
		py1iAct = QAction('&Goto pinyin1 index', self)
		pyciAct = QAction('&Goto pinyincommon index', self)
		clearAct = QAction('&Clear', self)
		clearAllAct = QAction('&Clear all', self)
		commitAct = QAction('&commit to database', self)
		wordAct = QAction('&submitWord', self)
		redoAct = QAction('&redoWord', self)

		exitAct.setShortcut('Ctrl+`')
		exitAct.triggered.connect(qApp.quit)

		tableAct.setShortcut('Ctrl+0')
		tableAct.triggered.connect(self.sourceCreateWidget.focusTable)
		hanAct.setShortcut('Ctrl+O')
		hanAct.triggered.connect(self.sourceCreateWidget.focusHan)
		py1Act.setShortcut('Ctrl+J')
		py1Act.triggered.connect(self.sourceCreateWidget.focusPy1)
		pycAct.setShortcut('Ctrl+N')
		pycAct.triggered.connect(self.sourceCreateWidget.focusPyc)
		clearAct.setShortcut('Ctrl+-')
		clearAct.triggered.connect(self.sourceCreateWidget.clear)
		clearAllAct.setShortcut('Ctrl+=')
		clearAllAct.triggered.connect(self.sourceCreateWidget.clearall)
		wordAct.setShortcut('Ctrl+P')
		wordAct.triggered.connect(self.sourceCreateWidget.addWord)
		commitAct.setShortcut('Ctrl+\\')
		commitAct.triggered.connect(self.sourceCreateWidget.insert)
		redoAct.setShortcut('Ctrl+S')
		redoAct.triggered.connect(self.sourceCreateWidget.redoWord)
		py1iAct.setShortcut('Ctrl+K')
		py1iAct.triggered.connect(self.sourceCreateWidget.focusPy1i)
		pyciAct.setShortcut('Ctrl+M')
		pyciAct.triggered.connect(self.sourceCreateWidget.focusPyci)
		self.menuBar().clear()
		menubar = self.menuBar()
		toolMenu = menubar.addMenu('&Tool')
		toolMenu.addAction(exitAct)
		toolMenu.addAction(tableAct)
		toolMenu.addAction(hanAct)
		toolMenu.addAction(py1Act)
		toolMenu.addAction(pycAct)
		toolMenu.addAction(py1iAct)
		toolMenu.addAction(pyciAct)
		toolMenu.addAction(commitAct)
		toolMenu.addAction(wordAct)
		toolMenu.addAction(clearAct)
		toolMenu.addAction(clearAllAct)
		toolMenu.addAction(redoAct)
def main():
	
	app = QApplication(sys.argv)
	f = QFont("微软雅黑")
	f.setPixelSize(f.pointSize()*1.5)
	app.setFont(f)
	app.setStyle(QStyleFactory.create('Fusion'))
	ex = MainWindow()
	sys.exit(app.exec_())
	cur.close()
	conn.close()
if __name__ == '__main__':
	main()